
global firststage 			0 // 1: run, 0: not  
global robustness 			0 // 1: run, 0: not  
global additionalcontrol 	0 // 1: run, 0: not  
global googlenetwork		0 // 1: run, 0: not  
global fit					0 // 1: run, 0: not  

*------------------------------------------------------------------------------**------------------------------------------------------------------------------*
* firststage *
*------------------------------------------------------------------------------**------------------------------------------------------------------------------*

if $firststage == 1 {
	
use "data/working/working_platform.dta", clear
sort household_id currency_id
*cf _all using "data/working/working_sharepost_Jiarui.dta"

*drop _merge
*merge m:1 date currency_id using "data/working/close"
*drop if _merge == 2
*drop _merge

*------------------------------------------------------------------------------*
* Prepare for sample (temporarily "append" the two dataset together) 
*------------------------------------------------------------------------------*
*----- Survey Sample
duplicates drop currency_id date,force

* Generate indicator
gen survey = 1

save "data/working/survey_tem",replace


*----- Full Sample
use "data/working/cryptocurrency",clear
gen year = year(date)
gen logME = log(capmrktcurusd)
gen survey = 0

save "data/working/real_tem",replace


*----- "Temporary" append for regression
use "data/working/survey_tem",clear
append using "data/working/real_tem"


*----- Gen dummy for currency fixed effect
drop dcurrency_id
encode currency_id, gen(dcurrency_id)
replace logsupply = log(supply) 
gen logprice = log(price) 
gen logsupply_w = log(supply_w)
gen logprice_w = log(price_w)

su logsupply logsupply_w if year == 2018 
su logprice logprice_w if year == 2018 

save "data/working/analysis",replace


*------------------------------------------------------------------------------*
* First Stage Regression
*------------------------------------------------------------------------------*
use "data/working/analysis", clear
gen const = 1

*----- Survey Sample
reg logME logsupply beta momentum4 incomenewdummy agenewdummy countrynewdummy type_objective log_sp500 log_libor if survey==1 , robust
	est save "output/estimates/tableB1a_col1.ster"	 ,replace	
ivreg2 const (logME = logsupply) beta momentum4 incomenewdummy agenewdummy countrynewdummy type_objective log_sp500 log_libor  if survey==1 , robust ffirst
	est save "output/estimates/tableB1a_col1_Fstat.ster"	 ,replace	

reg logME logsupply beta momentum4 incomenewdummy agenewdummy countrynewdummy type_objective log_sp500 log_libor i.wave if survey==1 , robust
	est save "output/estimates/tableB1a_col2.ster"	 ,replace	
ivreg2 const (logME = logsupply) beta momentum4 incomenewdummy agenewdummy countrynewdummy type_objective log_sp500 log_libor i.wave if survey==1 , robust ffirst
	est save "output/estimates/tableB1a_col2_Fstat.ster"	 ,replace	

reg logME logsupply beta momentum4 incomenewdummy agenewdummy countrynewdummy type_objective log_sp500 log_libor i.date if survey==1 , robust
	est save "output/estimates/tableB1a_col3.ster"	 ,replace	
ivreg2 const (logME = logsupply) beta momentum4 incomenewdummy agenewdummy countrynewdummy type_objective log_sp500 log_libor i.date if survey==1 , robust ffirst
	est save "output/estimates/tableB1a_col3_Fstat.ster"	 ,replace	


*----- Real Sample
reg logME logsupply if survey==0 & year == 2018, robust
	est save "output/estimates/tableB1b_col1.ster"	 ,replace	
ivreg2 const (logME = logsupply) if survey==0 & year == 2018, robust ffirst
	est save "output/estimates/tableB1b_col1_Fstat.ster"	 ,replace	

reg logME logsupply i.date if survey==0 & year == 2018, robust
	est save "output/estimates/tableB1b_col2.ster"	 ,replace	
ivreg2 const (logME = logsupply) i.date if survey==0 & year == 2018, robust ffirst
	est save "output/estimates/tableB1b_col2_Fstat.ster"	 ,replace	

reg logME logsupply i.date i.dcurrency_id if survey==0 & year == 2018, robust
	est save "output/estimates/tableB1b_col3.ster"	 ,replace	
ivreg2 const (logME = logsupply) i.date i.dcurrency_id if survey==0 & year == 2018, robust ffirst
	est save "output/estimates/tableB1b_col3_Fstat.ster"	 ,replace	



*----- Survey Sample
reg logprice logsupply beta momentum4 incomenewdummy agenewdummy countrynewdummy type_objective log_sp500 log_libor  if survey==1 , robust
	est save "output/estimates/tableB1a_col4.ster"	 ,replace	
ivreg2 const (logprice = logsupply) beta momentum4 incomenewdummy agenewdummy countrynewdummy type_objective log_sp500 log_libor  if survey==1 , robust ffirst
	est save "output/estimates/tableB1a_col4_Fstat.ster"	 ,replace	

reg logprice logsupply beta momentum4 incomenewdummy agenewdummy countrynewdummy type_objective log_sp500 log_libor i.wave if survey==1 , robust
	est save "output/estimates/tableB1a_col5.ster"	 ,replace	
ivreg2 const (logprice = logsupply) beta momentum4 incomenewdummy agenewdummy countrynewdummy type_objective log_sp500 log_libor i.wave if survey==1 , robust ffirst
	est save "output/estimates/tableB1a_col5_Fstat.ster"	 ,replace	

reg logprice logsupply beta momentum4 incomenewdummy agenewdummy countrynewdummy type_objective log_sp500 log_libor  i.date if survey==1 , robust
	est save "output/estimates/tableB1a_col6.ster"	 ,replace	
ivreg2 const (logprice = logsupply) beta momentum4 incomenewdummy agenewdummy countrynewdummy type_objective log_sp500 log_libor i.date if survey==1 , robust ffirst
	est save "output/estimates/tableB1a_col6_Fstat.ster"	 ,replace	


*----- Real Sample
reg logprice logsupply if survey==0 & year == 2018, robust
	est save "output/estimates/tableB1b_col4.ster"	 ,replace	
ivreg2 const (logprice = logsupply) if survey==0 & year == 2018, robust ffirst
	est save "output/estimates/tableB1b_col4_Fstat.ster"	 ,replace	

reg logprice logsupply i.date if survey==0 & year == 2018, robust
	est save "output/estimates/tableB1b_col5.ster"	 ,replace	
ivreg2 const (logprice = logsupply) i.date if survey==0 & year == 2018, robust ffirst
	est save "output/estimates/tableB1b_col5_Fstat.ster"	 ,replace	

reg logprice logsupply i.date i.dcurrency_id if survey==0 & year == 2018, robust
	est save "output/estimates/tableB1b_col6.ster"	 ,replace	
ivreg2 const (logprice = logsupply) i.date i.dcurrency_id if survey==0 & year == 2018, robust ffirst
	est save "output/estimates/tableB1b_col6_Fstat.ster"	 ,replace	

					 
}


*------------------------------------------------------------------------------**------------------------------------------------------------------------------*
* robustness *
*------------------------------------------------------------------------------**------------------------------------------------------------------------------*

if $robustness == 1 {
	
use "data/working/working_platform.dta",clear

*----- Different weights
// w/ iv w/ expectation	+ beta + momentum 
gmm (dep_weighted_bygroup * exp(-{xb: logME beta momentum4 pow short_increase long_decrease ///
		currencies_potential incomenewdummy agenewdummy countrynewdummy type_objective log_sp500 log_libor _cons}) -1 ), ///
		vce(cl household_id) tech(bfgs) ///  gn, nr, dfp, bfgs winitial(identity) 
		instruments( logsupply beta momentum4 pow short_increase long_decrease ///
		currencies_potential incomenewdummy agenewdummy countrynewdummy type_objective log_sp500 log_libor) ///	
		from(logME .3  ///
		 pow 0.9  _cons 0) conv_maxiter(100)	 
est save "output/estimates/tableB2_col1.ster"	 ,replace		  
		 		 	 

// w/ iv w/ expectation	+ beta + momentum 
gmm (dep_weighted_equal * exp(-{xb: logME beta momentum4 pow short_increase long_decrease ///
		currencies_potential incomenewdummy agenewdummy countrynewdummy type_objective log_sp500 log_libor _cons}) -1 ), ///
		vce(cl household_id) tech(bfgs) ///  gn, nr, dfp, bfgs winitial(identity) 
		instruments( logsupply beta momentum4 pow short_increase long_decrease ///
		currencies_potential incomenewdummy agenewdummy countrynewdummy type_objective log_sp500 log_libor) ///	
		from(logME .3  ///
		 pow 0.9  _cons 0) conv_maxiter(100)	 
est save "output/estimates/tableB2_col2.ster"	 ,replace		  
		 		 	 


*---------- Wealth-Income Ratio
// w/ iv w/ expectation	+ beta + momentum 
gmm (dep_weighted_robust0 * exp(-{xb: logME beta momentum4 pow short_increase long_decrease ///
		currencies_potential incomenewdummy agenewdummy countrynewdummy type_objective log_sp500 log_libor _cons}) -1 ), ///
		vce(cl household_id) tech(bfgs) ///  gn, nr, dfp, bfgs winitial(identity) 
		instruments( logsupply beta momentum4 pow short_increase long_decrease ///
		currencies_potential incomenewdummy agenewdummy countrynewdummy type_objective log_sp500 log_libor) ///	
		from(  _cons 0) conv_maxiter(100)	 
est save "output/estimates/tableB2_col3.ster"	 ,replace				  
		 		 	 

// w/ iv w/ expectation	+ beta + momentum 
gmm (dep_weighted_robust1 * exp(-{xb: logME beta momentum4 pow short_increase long_decrease ///
		currencies_potential incomenewdummy agenewdummy countrynewdummy type_objective log_sp500 log_libor _cons}) -1 ), ///
		vce(cl household_id) tech(bfgs) ///  gn, nr, dfp, bfgs winitial(identity) 
		instruments( logsupply beta momentum4 pow short_increase long_decrease ///
		currencies_potential incomenewdummy agenewdummy countrynewdummy type_objective log_sp500 log_libor) ///	
		from(  _cons 0) conv_maxiter(100)	 
est save "output/estimates/tableB2_col4.ster"	 ,replace				  
		 		 	 

// w/ iv w/ expectation	+ beta + momentum 
gmm (dep_weighted_robust2 * exp(-{xb: logME beta momentum4 pow short_increase long_decrease ///
		currencies_potential incomenewdummy agenewdummy countrynewdummy type_objective log_sp500 log_libor _cons}) -1 ), ///
		vce(cl household_id) tech(bfgs) ///  gn, nr, dfp, bfgs winitial(identity) 
		instruments( logsupply beta momentum4 pow short_increase long_decrease ///
		currencies_potential incomenewdummy agenewdummy countrynewdummy type_objective log_sp500 log_libor) ///	
		from(  _cons 0) conv_maxiter(100)	 
est save "output/estimates/tableB2_col5.ster"	 ,replace				  
		 		 	 

// w/ iv w/ expectation	+ beta + momentum 
gmm (dep_weighted_robust3 * exp(-{xb: logME beta momentum4 pow short_increase long_decrease ///
		currencies_potential incomenewdummy agenewdummy countrynewdummy type_objective log_sp500 log_libor _cons}) -1 ), ///
		vce(cl household_id) tech(bfgs) ///  gn, nr, dfp, bfgs winitial(identity) 
		instruments( logsupply beta momentum4 pow short_increase long_decrease ///
		currencies_potential incomenewdummy agenewdummy countrynewdummy type_objective log_sp500 log_libor) ///	
		from(  _cons 0) conv_maxiter(100)	 
est save "output/estimates/tableB2_col6.ster"	 ,replace				  
		 		 	 

	
}	


*------------------------------------------------------------------------------**------------------------------------------------------------------------------*
* additionalcontrol *
*------------------------------------------------------------------------------**------------------------------------------------------------------------------*

if $additionalcontrol == 1 {

use "data/working/working_platform.dta",clear
set seed 9

	
*--- (1) Short decrease
gmm (dep_weighted * exp(-{xb: logME beta momentum4 pow short_increase short_decrease long_decrease ///
		currencies_potential incomenewdummy agenewdummy countrynewdummy type_objective log_sp500 log_libor _cons}) -1 ), ///
		vce(cl household_id) tech(bfgs) ///  gn, nr, dfp, bfgs winitial(identity) 
		instruments( logsupply beta momentum4 pow short_increase short_decrease long_decrease ///
		currencies_potential incomenewdummy agenewdummy countrynewdummy type_objective log_sp500 log_libor) ///	
		 conv_maxiter(100)	
est save "output/estimates/tableB3_col1.ster"	 ,replace			


*--- (2) Wave dummy
gen dwave = wave - 1
gmm (dep_weighted * exp(-{xb: logME beta momentum4 pow short_increase long_decrease ///
		currencies_potential incomenewdummy agenewdummy countrynewdummy type_objective log_sp500 log_libor dwave _cons}) -1 ), ///
		vce(cl household_id) tech(bfgs) ///  gn, nr, dfp, bfgs winitial(identity) 
		instruments( logsupply beta momentum4 pow short_increase short_decrease long_decrease ///
		currencies_potential incomenewdummy agenewdummy countrynewdummy type_objective log_sp500 log_libor dwave) ///	
		 conv_maxiter(100)	
est save "output/estimates/tableB3_col2.ster"	 ,replace			


*--- (3) company_consider_implement
gen companyconsider = company_consider_implement == "Yes"
gmm (dep_weighted * exp(-{xb: logME beta momentum4 pow short_increase long_decrease ///
		currencies_potential incomenewdummy agenewdummy countrynewdummy type_objective log_sp500 log_libor companyconsider _cons}) -1 ), ///
		vce(cl household_id) tech(bfgs) ///  gn, nr, dfp, bfgs winitial(identity) 
		instruments( logsupply beta momentum4 pow short_increase long_decrease ///
		currencies_potential incomenewdummy agenewdummy countrynewdummy type_objective log_sp500 log_libor companyconsider) ///	
		 conv_maxiter(100)	
est save "output/estimates/tableB3_col3.ster"	 ,replace			


*--- (4) Without Ripple + Remove PoW dummy
preserve
drop if currency_id == "ripple"

gmm (dep_weighted * exp(-{xb: logME beta momentum4 short_increase long_decrease ///
		currencies_potential incomenewdummy agenewdummy countrynewdummy type_objective log_sp500 log_libor _cons}) -1 ), ///
		vce(cl household_id) tech(bfgs) ///  gn, nr, dfp, bfgs winitial(identity) 
		instruments( logsupply beta momentum4 short_increase long_decrease ///
		currencies_potential incomenewdummy agenewdummy countrynewdummy type_objective log_sp500 log_libor) ///	
		from(logME .1  ///
		   _cons 0) conv_maxiter(100)	 
est save "output/estimates/tableB3_col4.ster"	 ,replace			
restore


*--- (5) with age since first foundation 

gen foundation = ym(2009,1) if currency_id == "bitcoin"
replace foundation = ym(2017,7) if currency_id == "bitcoin_cash"
replace foundation = ym(2014,1) if currency_id == "dash"
replace foundation = ym(2015,7) if currency_id == "ethereum"
replace foundation = ym(2011,10) if currency_id == "litecoin"
replace foundation = ym(2014,4) if currency_id == "monero"
replace foundation = ym(2012,6) if currency_id == "ripple"
replace foundation = ym(2016,10) if currency_id == "zcash"
replace foundation = (ym(2018,1) + ym(2012,7))/2 if currency_id == "ALT"

gen cage = ym(2018,1) - foundation

gmm (dep_weighted * exp(-{xb: logME beta momentum4 pow short_increase long_decrease ///
		currencies_potential incomenewdummy agenewdummy countrynewdummy type_objective log_sp500 log_libor cage _cons}) -1 ), ///
		vce(cl household_id) tech(bfgs) ///  gn, nr, dfp, bfgs winitial(identity) 
		instruments( logsupply beta momentum4 pow short_increase long_decrease ///
		currencies_potential incomenewdummy agenewdummy countrynewdummy type_objective log_sp500 log_libor cage) ///	
 conv_maxiter(100)	 
est save "output/estimates/tableB3_col5.ster"	 ,replace			



*--- (6) with age since white paper

gen whitepaper = ym(2008,10) if currency_id == "bitcoin"
replace whitepaper = ym(2008,10) if currency_id == "bitcoin_cash"
replace whitepaper = ym(2014,1) if currency_id == "dash"
replace whitepaper = ym(2014,12) if currency_id == "ethereum"
replace whitepaper = ym(2011,10) if currency_id == "litecoin"
replace whitepaper = ym(2013,10) if currency_id == "monero"
replace whitepaper = ym(2012,6) if currency_id == "ripple"
replace whitepaper = ym(2014,5) if currency_id == "zcash"
replace whitepaper = (ym(2018,6) + ym(2013,10))/2 if currency_id == "ALT"

gen cage_whitepaper = ym(2018,1) - whitepaper

// w/ iv w/ expectation	+ beta + momentum 
gmm (dep_weighted * exp(-{xb: logME beta momentum4 pow short_increase long_decrease ///
		currencies_potential incomenewdummy agenewdummy countrynewdummy type_objective log_sp500 log_libor cage_whitepaper _cons}) -1 ), ///
		vce(cl household_id) tech(bfgs) ///  gn, nr, dfp, bfgs winitial(identity) 
		instruments( logsupply beta momentum4 pow short_increase long_decrease ///
		currencies_potential incomenewdummy agenewdummy countrynewdummy type_objective log_sp500 log_libor cage_whitepaper) ///	
 conv_maxiter(100)	 
est save "output/estimates/tableB3_col6.ster"	 ,replace			



*--- (7) Additional interaction	
// w/ iv w/ expectation	+ beta + momentum 

gmm (dep_weighted * exp(-{xb: logME beta momentum4 pow short_increase long_decrease ///
		currencies_potential incomenewdummy agenewdummy countrynewdummy type_objective log_sp500 log_libor 1.short_increase#1.long_decrease 1.short_increase#1.currencies_potential 1.long_decrease#1.currencies_potential 1.short_increase#1.long_decrease#1.currencies_potential _cons}) -1 ), ///
		vce(cl household_id) tech(bfgs) ///  gn, nr, dfp, bfgs winitial(identity) 
		instruments( logsupply beta momentum4 pow short_increase long_decrease ///
		currencies_potential incomenewdummy agenewdummy countrynewdummy type_objective log_sp500 log_libor 1.short_increase#1.long_decrease 1.short_increase#1.currencies_potential 1.long_decrease#1.currencies_potential 1.short_increase#1.long_decrease#1.currencies_potential) ///	
 conv_maxiter(100)	 
est save "output/estimates/tableB3_col7.ster"	 ,replace			



*--- (8) With additional belief variable. Average currency potential at the individual level for other currency
foreach v of var currencies_potential {
	bys household_id: egen sum_`v' = sum(`v')
	gen ot_`v' = sum_`v' - `v'
	gen aot_`v' = ot_`v' / 8
}

gmm (dep_weighted * exp(-{xb: logME beta momentum4 pow short_increase long_decrease ///
		currencies_potential incomenewdummy agenewdummy countrynewdummy type_objective log_sp500 log_libor ot_currencies_potential _cons}) -1 ), ///
		vce(cl household_id) tech(bfgs) ///  gn, nr, dfp, bfgs winitial(identity) 
		instruments( logsupply beta momentum4 pow short_increase long_decrease ///
		currencies_potential incomenewdummy agenewdummy countrynewdummy type_objective log_sp500 log_libor ot_currencies_potential) ///	
 conv_maxiter(100)	
est save "output/estimates/tableB3_col8.ster"	 ,replace			


}


*------------------------------------------------------------------------------**------------------------------------------------------------------------------*
* googlenetwork *
*------------------------------------------------------------------------------**------------------------------------------------------------------------------*

if $googlenetwork == 1 {
	
*----- Google Search 
use "data/working/working_platform.dta",clear
set seed 9	

drop _merge
merge m:1 date currency_id using "data/working/google_value"
drop if _merge == 2
drop _merge
merge m:1 date currency_id using "data/working/google_Hack"
drop if _merge == 2
drop _merge

merge m:1 date using "data/working/google_value_Bitcoin"
drop if _merge == 2
drop _merge
merge m:1 date using "data/working/google_hack_Bitcoin"
drop if _merge == 2
drop _merge


*- (1) Bitcoin Same day
gmm (dep_weighted * exp(-{xb: logME beta momentum4 pow short_increase long_decrease ///
		currencies_potential incomenewdummy agenewdummy countrynewdummy type_objective log_sp500 log_libor dgoogle_value_Bitcoin _cons}) -1 ), ///
		vce(cl household_id) tech(bfgs) ///  gn, nr, dfp, bfgs winitial(identity) 
		instruments( logsupply beta momentum4 pow short_increase long_decrease ///
		currencies_potential incomenewdummy agenewdummy countrynewdummy type_objective log_sp500 log_libor dgoogle_value_Bitcoin) ///	
 conv_maxiter(100)	 
est save "output/estimates/tableB4_col1.ster"	 ,replace				  
		 		 	 

*- (2) Bitcoin Hack Same day
gmm (dep_weighted * exp(-{xb: logME beta momentum4 pow short_increase long_decrease ///
		currencies_potential incomenewdummy agenewdummy countrynewdummy type_objective log_sp500 log_libor google_hack_Bitcoin _cons}) -1 ), ///
		vce(cl household_id) tech(bfgs) ///  gn, nr, dfp, bfgs winitial(identity) 
		instruments( logsupply beta momentum4 pow short_increase long_decrease ///
		currencies_potential incomenewdummy agenewdummy countrynewdummy type_objective log_sp500 log_libor google_hack_Bitcoin) ///	
		from(logME .1  ///
		 pow 0.1  _cons 0) conv_maxiter(100)	 
est save "output/estimates/tableB4_col2.ster"	 ,replace				  
		 		 	 

					 
*- (3) Bitcoin Lag 7 Days
gmm (dep_weighted * exp(-{xb: logME beta momentum4 pow short_increase long_decrease ///
		currencies_potential incomenewdummy agenewdummy countrynewdummy type_objective log_sp500 log_libor l7_dgoogle_value_Bitcoin _cons}) -1 ), ///
		vce(cl household_id) tech(bfgs) ///  gn, nr, dfp, bfgs winitial(identity) 
		instruments( logsupply beta momentum4 pow short_increase long_decrease ///
		currencies_potential incomenewdummy agenewdummy countrynewdummy type_objective log_sp500 log_libor l7_dgoogle_value_Bitcoin) ///	
 conv_maxiter(100)	 
est save "output/estimates/tableB4_col3.ster"	 ,replace				  
		 		 	 

*- (4) Bitcoin Hack Lag 7 Days
gmm (dep_weighted * exp(-{xb: logME beta momentum4 pow short_increase long_decrease ///
		currencies_potential incomenewdummy agenewdummy countrynewdummy type_objective log_sp500 log_libor l7_google_hack_Bitcoin _cons}) -1 ), ///
		vce(cl household_id) tech(bfgs) ///  gn, nr, dfp, bfgs winitial(identity) 
		instruments( logsupply beta momentum4 pow short_increase long_decrease ///
		currencies_potential incomenewdummy agenewdummy countrynewdummy type_objective log_sp500 log_libor l7_google_hack_Bitcoin) ///	
		 conv_maxiter(100)	 
est save "output/estimates/tableB4_col4.ster"	 ,replace				  


	
*----- Network Address
use "data/working/working_platform.dta",clear
set seed 9	
drop _merge
merge m:1 date currency_id using "data/working/num_activeaddress"
drop if _merge == 2
drop _merge 
merge m:1 date using "data/working/num_activeaddress_Bitcoin"
drop if _merge == 2
drop _merge 
replace num_activeaddress = num_activeaddress_Bitcoin if num_activeaddress == .
replace L7_num_activeaddress = L7_num_activeaddress_Bitcoin if L7_num_activeaddress == .
replace L14_num_activeaddress = L14_num_activeaddress_Bitcoin if L14_num_activeaddress == .
*drop num_activeaddress_Bitcoin

merge m:1 date currency_id using "data/working/num_newaddress"
drop if _merge == 2
drop _merge 
merge m:1 date using "data/working/num_newaddress_Bitcoin"
drop if _merge == 2
drop _merge 
replace num_newaddress = num_newaddress_Bitcoin if num_newaddress == .
replace L7_num_newaddress = L7_num_newaddress_Bitcoin if L7_num_newaddress == .
replace L14_num_newaddress = L14_num_newaddress_Bitcoin if L14_num_newaddress == .
*drop num_newaddress_Bitcoin

* Log
gen log_num_activeaddress_Bitcoin = log(num_activeaddress_Bitcoin) 
gen log_num_newaddress_Bitcoin = log(num_newaddress_Bitcoin)  
gen log_num_activeaddress = log(num_activeaddress)  
gen log_num_newaddress = log(num_newaddress)  

gen d_log_num_activeaddress_Bitcoin = log(num_activeaddress_Bitcoin) - log(L7_num_activeaddress_Bitcoin) 
gen d_log_num_newaddress_Bitcoin = log(num_newaddress_Bitcoin) - log(L7_num_newaddress_Bitcoin)  
gen d_log_num_activeaddress = log(num_activeaddress) - log(L7_num_activeaddress)  
gen d_log_num_newaddress = log(num_newaddress) - log(L7_num_newaddress)  

gen d2_num_activeaddress_Bitcoin = (num_activeaddress_Bitcoin - L7_num_activeaddress_Bitcoin) / 1000000
gen d2_num_newaddress_Bitcoin = (num_newaddress_Bitcoin - L7_num_newaddress_Bitcoin) / 1000000 
gen d2_num_activeaddress = (num_activeaddress - L7_num_activeaddress)  / 1000000
gen d2_num_newaddress = (num_newaddress - L7_num_newaddress)  / 1000000


gen L7_log_num_activeaddress_BTC = log(L7_num_activeaddress_Bitcoin) 
gen L7_log_num_newaddress_BTC = log(L7_num_newaddress_Bitcoin)  
gen L7_log_num_activeaddress = log(L7_num_activeaddress)  
gen L7_log_num_newaddress = log(L7_num_newaddress) 

gen L7_d_log_num_activeaddress_BTC = log(L7_num_activeaddress_Bitcoin) - log(L14_num_activeaddress_Bitcoin) 
gen L7_d_log_num_newaddress_BTC = log(L7_num_newaddress_Bitcoin) - log(L14_num_newaddress_Bitcoin)  
gen L7_d_log_num_activeaddress = log(L7_num_activeaddress) - log(L14_num_activeaddress)  
gen L7_d_log_num_newaddress = log(L7_num_newaddress) - log(L14_num_newaddress)  

gen L7_d2_num_activeaddress_BTC = (L7_num_activeaddress_Bitcoin - L14_num_activeaddress_Bitcoin) / 1000000
gen L7_d2_num_newaddress_BTC = (L7_num_newaddress_Bitcoin - L14_num_newaddress_Bitcoin)  / 1000000
gen L7_d2_num_activeaddress = (L7_num_activeaddress - L14_num_activeaddress)  / 1000000
gen L7_d2_num_newaddress = (L7_num_newaddress - L14_num_newaddress)  / 1000000


* (5) Active Address Bitcoin
gmm (dep_weighted * exp(-{xb: logME beta momentum4 pow short_increase long_decrease ///
		currencies_potential incomenewdummy agenewdummy countrynewdummy type_objective log_sp500 log_libor d_log_num_activeaddress_Bitcoin _cons}) -1 ), ///
		vce(cl household_id) tech(bfgs) ///  gn, nr, dfp, bfgs winitial(identity) 
		instruments( logsupply beta momentum4 pow short_increase long_decrease ///
		currencies_potential incomenewdummy agenewdummy countrynewdummy type_objective log_sp500 log_libor d_log_num_activeaddress_Bitcoin) ///	
		 conv_maxiter(500)	 
est save "output/estimates/tableB4_col5.ster"	 ,replace				  



* (6) New Address Bitcoin
gmm (dep_weighted * exp(-{xb: logME beta momentum4 pow short_increase long_decrease ///
		currencies_potential incomenewdummy agenewdummy countrynewdummy type_objective log_sp500 log_libor d_log_num_newaddress_Bitcoin _cons}) -1 ), ///
		vce(cl household_id) tech(bfgs) ///  gn, nr, dfp, bfgs winitial(identity) 
		instruments( logsupply beta momentum4 pow short_increase long_decrease ///
		currencies_potential incomenewdummy agenewdummy countrynewdummy type_objective log_sp500 log_libor d_log_num_newaddress_Bitcoin) ///	
		conv_maxiter(500)	 
est save "output/estimates/tableB4_col6.ster"	 ,replace				  
	
	
	
	
}

	
*------------------------------------------------------------------------------**------------------------------------------------------------------------------*
* fit *
*------------------------------------------------------------------------------**------------------------------------------------------------------------------*

if $fit == 1 {	
	
*--- Split the sample
use "data/working/working_platform.dta",clear
keep household_id
duplicates drop household_id, force

di 4647 * 0.2
set seed 999
randomtag , count(929) gen(test)
tab test

save "data/working/split.dta", replace

use "data/working/working_platform.dta",clear
drop _merge
merge m:1 household_id using "data/working/split.dta"

save "data/working/working_platform_Split.dta",replace



use "data/working/working_platform_Split.dta",clear
set seed 999
*--------- Simple average		
// w/ iv w/o expectation : Change initial value + beta & momentum
gmm (dep_weighted * exp(-{xb: logME beta momentum4  pow incomenewdummy agenewdummy countrynewdummy type_objective log_sp500 log_libor _cons}) -1 ) if test == 0, ///
		vce(cl household_id) tech(bfgs) ///  gn, nr, dfp, bfgs winitial(identity) 
		instruments( logsupply beta momentum4 pow incomenewdummy agenewdummy countrynewdummy type_objective log_sp500 log_libor ) ///	
		 conv_maxiter(500)	 
est save "output/estimates/tableB5_col1.ster"	 ,replace				  
		 		 
		

// w/ iv w/o expectation : Change initial value + beta & momentum + only short-term
gmm (dep_weighted * exp(-{xb: logME beta momentum4  pow short_increase incomenewdummy agenewdummy countrynewdummy type_objective log_sp500 log_libor _cons}) -1 ) if test == 0, ///
		vce(cl household_id) tech(bfgs) ///  gn, nr, dfp, bfgs winitial(identity) 
		instruments( logsupply beta momentum4 pow short_increase incomenewdummy agenewdummy countrynewdummy type_objective log_sp500 log_libor ) ///	
		from(  _cons 0) conv_maxiter(100)	 
est save "output/estimates/tableB5_col2.ster"	 ,replace				  
	 

// w/ iv w/ expectation	+ beta + momentum  without currency_potential
gmm (dep_weighted * exp(-{xb: logME beta momentum4 pow short_increase long_decrease ///
		 incomenewdummy agenewdummy countrynewdummy type_objective log_sp500 log_libor _cons}) -1 ) if test == 0, ///
		vce(cl household_id) tech(bfgs) ///  gn, nr, dfp, bfgs winitial(identity) 
		instruments( logsupply beta momentum4 pow short_increase long_decrease ///
	    incomenewdummy agenewdummy countrynewdummy type_objective log_sp500 log_libor)  ///	
		from(  _cons 0) conv_maxiter(100)	 
est save "output/estimates/tableB5_col3.ster"	 ,replace				  
		 		 	 

// w/ iv w/ expectation	+ beta + momentum 
gmm (dep_weighted * exp(-{xb: logME beta momentum4 pow short_increase long_decrease ///
		currencies_potential incomenewdummy agenewdummy countrynewdummy type_objective log_sp500 log_libor _cons}) -1 ) if test == 0, ///
		vce(cl household_id) tech(bfgs) ///  gn, nr, dfp, bfgs winitial(identity) 
		instruments( logsupply beta momentum4 pow short_increase long_decrease ///
		currencies_potential incomenewdummy agenewdummy countrynewdummy type_objective log_sp500 log_libor) ///	
		from( _cons 0) conv_maxiter(100)	 
est save "output/estimates/tableB5_col4.ster"	 ,replace				  	
	
}	
